﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using log4net;
using CashFlow.Db.DAL;
using CashFlow.Db.Entity;
using SysAdmin._00_Def;

namespace SysAdmin._03_Service._00_Db
{
    public class ShopService
    {
        public static IList<ShopInfoEntity> QueryAll()
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                return db.Queryable<ShopInfoEntity>().ToList();
            }
        }

        public static IList<UserInfoAndShopInfoEntity> QueryAllShopInfoAndUserInfo()
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                return db.Queryable<ShopInfoEntity>()
                    .InnerJoin<UserInfoEntity>((s, u) => s.ShopManagerUserId == u.Id)
                    .Select((s, u) => new UserInfoAndShopInfoEntity
                    {
                        ShopInfo = s,
                        UserInfo = u
                    })
                    .ToList();
            }
        }

        public static ShopInfoEntity QueryShopInfoByReferenceInvitationCode(string invitationCode)
        {
            ShopInfoEntity si = null;

            string tmpInvitationCode = invitationCode;
            using (var db = SqlSugarHelper.GetDb())
            {
                while (true)
                {
                    var us = db.Queryable<UserInfoEntity>()
                        .LeftJoin<ShopInfoEntity>((u, s) => u.Id == s.ShopManagerUserId)
                        .Where((u, s) => u.InvitationCode == tmpInvitationCode)
                        .Select((u, s) => new { UserInfo = u, ShopInfo = s })
                        .First();
                    if (us != null)
                    {
                        if (us.ShopInfo != null && us.ShopInfo.Id > 0)
                        {
                            si = us.ShopInfo;
                            break;
                        }

                        if (us.UserInfo != null)
                        {
                            tmpInvitationCode = us.UserInfo.ReferenceInvitationCode;
                        }
                    }
                    else
                    {
                        break;
                    }
                }
            }

            return si;
        }

        public static ShopInfoEntity QueryShopInfoByUserId(int userId)
        {
            ShopInfoEntity si = null;

            using (var db = SqlSugarHelper.GetDb())
            {
                var entity = db.Queryable<ShopInfoEntity>().Where(x => x.ShopManagerUserId == userId).First();
                if (entity != null)
                {
                    return entity;
                }
                else
                {
                    var ui = db.Queryable<UserInfoEntity>().Where(x => x.Id == userId).First();
                    if (ui != null)
                    {
                        string invitationCode = ui.InvitationCode;
                        while (true)
                        {
                            var us = db.Queryable<UserInfoEntity>()
                                .LeftJoin<ShopInfoEntity>((u, s) => u.Id == s.ShopManagerUserId)
                                .Where((u, s) => u.InvitationCode == invitationCode)
                                .Select((u, s) => new { UserInfo = u, ShopInfo = s })
                                .First();
                            if (us != null)
                            {
                                if (us.ShopInfo != null && us.ShopInfo.Id > 0)
                                {
                                    si = us.ShopInfo;
                                    break;
                                }

                                if (us.UserInfo != null)
                                {
                                    invitationCode = us.UserInfo.ReferenceInvitationCode;
                                }
                            }
                            else
                            {
                                break;
                            }
                        }
                    }
                }
            }

            return si;
        }

        public static bool AddShop(string name
            , UserInfoEntity ShopManagerUserInfo
            , IList<UserInfoEntity> ShopUserInfoList)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int count = db.Queryable<ShopInfoEntity>()
                    .Where(x => x.Name == name).Count();
                if (count == 0)
                {
                    var su = new ShopInfoEntity
                    {
                        CreateTime = DateTime.Now,
                        Name = name,
                        ShopManagerUserId = ShopManagerUserInfo.Id
                    };
                    db.Insertable(su).ExecuteCommand();

                    var userIds = ShopUserInfoList.Select(x => x.Id).ToList();
                    db.Deleteable<ShopUserEntity>().Where(x => userIds.Contains(x.UserId)).ExecuteCommand();
                    var ls = ShopUserInfoList.Select(x => new ShopUserEntity
                    {
                        ShopManagerUserId = ShopManagerUserInfo.Id,
                        UserId = x.Id
                    }).ToList();
                    db.Insertable<ShopUserEntity>(ls).ExecuteCommand();

                    return true;
                }
                else
                {
                    throw new MyException("店铺名称已存在！");
                }
            }
        }

        public static bool DeleteShop(int shopId)
        {
            using (var db = SqlSugarHelper.GetDb())
            {
                int n = db.Deleteable<ShopInfoEntity>(shopId).ExecuteCommand();
                return n > 0;
            }
        }
    }
}
